//
//  CommodityInfoViewController.m
//  CommodityManager
//
//  Created by lanou3g on 15/5/29.
//  Copyright (c) 2015年 libertyair. All rights reserved.
//

#import "CommodityInfoViewController.h"

#import "CommodityTableViewCell.h"
@interface CommodityInfoViewController ()



@end

@implementation CommodityInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title=@"新增商品";
    //设置代理
    _nameTextField.delegate=self;
    _numberTextfield.delegate=self;
    _tradePriceTextField.delegate=self;
    _retailPriceTextField.delegate=self;
    _categoryTextField.delegate=self;
    
//给imgView添加长按手势
    //打开交互
    self.imgView.userInteractionEnabled = YES;
    UILongPressGestureRecognizer * longPressCG = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(addImage)];
    [self.imgView addGestureRecognizer:longPressCG];
 //设置保存按钮
    
    UIBarButtonItem*rightBI=[[UIBarButtonItem alloc] initWithTitle:@"保存" style: UIBarButtonItemStylePlain target:self action:@selector(keepInfo)];
    self.navigationItem.rightBarButtonItem=rightBI;
    
    
    
}



//取消第一响应者,回收键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    return [textField resignFirstResponder];
}



-(void)textFieldDidBeginEditing:(UITextField *)textField
{

    CGRect frame=textField.frame;
   float offset=frame.origin.y+32-(self.view.frame.size.height-216.0);//键盘高度是216
    
    
    if (offset>0) {
       self.view.frame=CGRectMake(0, -offset, self.view.frame.size.width, self.view.frame.size.height);
    }
    
    
}




-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.view.frame=CGRectMake(0,65, self.view.frame.size.width, self.view.frame.size.height);
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//点击保存按钮
-(void)keepInfo
{
    CommodityTableViewCell*commodityCell=[[CommodityTableViewCell alloc] init];
    
    if ([_nameTextField.text isEqualToString:@""]&&[_numberTextfield.text isEqualToString:@""]) {
        UIAlertView*alert=[[UIAlertView alloc] initWithTitle:@"添加商品失败" message:@"商品名或货号不能为空" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
        [alert show];
    }else if ([_numberTextfield.text isEqualToString:commodityCell.numberLabel.text])
    {
        
        UIAlertView*alert1=[[UIAlertView alloc] initWithTitle:@"该货号已存在,请核实" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
        [alert1 show];
        
        
    }else
    {
        UIAlertView*alert2=[[UIAlertView alloc] initWithTitle:@"添加商品成功" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
        [alert2 show];
    }
}

//添加图片
-(void)addImage
{
    UIImagePickerController * imgPC = [[UIImagePickerController alloc] init];
    imgPC.delegate = self;
    imgPC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imgPC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    imgPC.allowsEditing = YES;
    [self presentViewController:imgPC animated:YES completion:nil];
    
}
//选取完成
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    //image为选中图片
    
    
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
// 取消选取
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}










/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
